<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Attach_mdl extends MY_Model {

	const DB_ATTACH = 'attach';				//附件数据库
	const DB_CAIPU_ATTACH = 'caipu_attach';		//菜谱图片数据库

    public function __construct()
    {
		parent::__construct();
		$this->table = 'attach';
    }
    //获取附件内容
    public function get_attach_by_id($id)
    {
    	$data = array();
		$this->db->select('*')->from(self::DB_ATTACH)->where('id', intval($id))->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			$data['time'] = date("Y-m-d h:i", $data['time']);
		}
		$query->free_result();
		
		return $data;
    }

    //获取附件列表
    public function get_attach_list($uid = NULL,$offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		if($uid && is_numeric($uid))
		{
			$this->db->where('uid', intval($uid));
		}
		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}
		$this->db->order_by($orderby); 
        return $this->db->get(self::DB_ATTACH);
    }

    //获取菜谱附件列表
    public function get_attach_caipu_list($uid = NULL,$offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		if($uid && is_numeric($uid))
		{
			$this->db->where('uid', intval($uid));
		}
		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}
		$this->db->order_by($orderby); 
        return $this->db->get(self::DB_CAIPU_ATTACH);
    }
    //搜索
    public function get_attach_search($keywords, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		$this->db->like('title', $this->db->escape_like_str($keywords));

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_ATTACH);
    }

    //搜索菜谱附件
    public function get_attach_caipu_search($keywords, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		$this->db->like('caipu_id', $this->db->escape_like_str($keywords));

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_CAIPU_ATTACH);
    }
	//***********************后台专用*********************************************************************
	public function attach_insert($arr)
	{
		$this->db->insert(self::DB_ATTACH,$arr);
		return $this->db->insert_id();
	}

	public function caipu_attach_insert($arr)
	{
		$this->db->insert(self::DB_CAIPU_ATTACH,$arr);
		return $this->db->insert_id();
	}

	public function attach_update($arr, $id)
	{
		return $this->db->update(self::DB_ATTACH, $arr, 'id = '.$id);
	}

	public function caipu_attach_update($arr, $id)
	{
		return $this->db->update(self::DB_CAIPU_ATTACH, $arr, 'id IN ('.$id.')');
	}

	public function attach_delete($id)
	{
		$upload_path = './uploads/';
		$this->db->select('*')->where('id', intval($id))->limit(1);
		$query = $this->db->get(self::DB_ATTACH)->row_array();
		if(file_exists($upload_path.$query['location'])) @unlink($upload_path.$query['location']);
		$this->load->library('alioss');
		$this->alioss->delete_object('ichufang',$query['location']);
		return $this->db->where('id',$id)->delete(self::DB_ATTACH);
	}

	public function attach_caipu_delete($id)
	{
		$upload_path = './uploads/';
		$this->db->select('*')->where('id', intval($id))->limit(1);
		$query = $this->db->get(self::DB_CAIPU_ATTACH)->row_array();
		if(file_exists($upload_path.$query['location'])) @unlink($upload_path.$query['location']);
		$this->load->library('alioss');
		$this->alioss->delete_object('ichufang',$query['location']);
		return $this->db->where('id',$id)->delete(self::DB_CAIPU_ATTACH);
	}

}

/* End of file attach_model.php */
/* Location: ./application/models/attach_model.php */